<template>
  <div>
    <el-form ref="form" label-width="80px">
      <el-form-item label="SPU名称">{{ spu.spuName }}</el-form-item>

      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>

      <el-form-item label="价格(元)">
        <el-input placeholder="价格(元)" type="number" v-model="skuInfo.price"></el-input>
      </el-form-item>

      <el-form-item label="重量(千克)">
        <el-input placeholder="重量(千克)" v-model="skuInfo.weight"></el-input>
      </el-form-item>

      <el-form-item label="规格描述">
        <el-input type="textarea" rows="4" v-model="skuInfo.skuDesc"></el-input>
      </el-form-item>

      <el-form-item label="平台属性">
        <el-form :inline="true" ref="form" label-width="80px">

          <el-form-item :label="attr.attrName" v-for="(attr, index) in attrInfoList" :key="attr.id">
            <!-- :value="{ attrId: attr.id, valueId: attrValue.id }" //对象写法不知道为啥不行
            v-model="attr.skuAttrValue"   :value="`${attr.id}:${attrValue.id}`"-->
            <el-select placeholder="请选择" v-model="attr.attrIdAndValueId">

              <el-option :label="attrValue.valueName" v-for="(attrValue, index) in attr.attrValueList"
                :value="`${attr.id}:${attrValue.id}`" :key="attrValue.id"> </el-option>
              <!-- 这里的value是要传给el-select中，value接收的，v-model是双向的意思 -->

            </el-select>
          </el-form-item>

        </el-form>
      </el-form-item>

      <el-form-item label="销售属性">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item :label="saleAttr.saleAttrName" v-for="(saleAttr, index) in spuSaleAttrList" :key="saleAttr.id">
            <el-select placeholder="请选择" v-model="saleAttr.attrIdAndValueId">
              <el-option v-for="(saleAttrValue, index) in saleAttr.spuSaleAttrValueList" :key="saleAttrValue.id"
                :label="saleAttrValue.saleAttrValueName" :value="`${saleAttr.id}:${saleAttrValue.id}`"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>

      <el-form-item label="图片列表">
        <el-table style="width: 100%" border :data="spuImageList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="80"> </el-table-column>

          <el-table-column prop="prop" label="图片" width="width">
            <template slot-scope="{ row }">
              <img :src="row.imgUrl" style="width: 100px; height: 100px" />
            </template>
          </el-table-column>

          <el-table-column prop="imgName" label="名称" width="width">
          </el-table-column>

          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row }">
              <el-button type="primary" v-if="row.isDefault === 0" @click="changeDefault(row)">设为默认</el-button>
              <el-button v-else>默认</el-button>
            </template>
          </el-table-column>

        </el-table>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
export default {
  name: "SkuForm",
  data() {
    return {
      spuImageList: [],
      spuSaleAttrList: [],
      attrInfoList: [],
      // 收集sku数据的字段
      skuInfo: {
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        skuName: "",
        price: 0,
        weight: "",
        skuDesc: "",
        skuDefaultImg: "",
        //收集图片
        skuImageList: [],
        skuAttrValueList: [],
        skuSaleAttrValueList: [],
      },
      spu: {},
      // 收集图片数据的字段
      imageList: [],
    };
  },
  methods: {
    async save() {
      //整理参数
      const { attrInfoList, skuInfo, spuSaleAttrList, imageList } = this;
      console.log('1');
      skuInfo.skuAttrValueList = attrInfoList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [attrId, valueId] = item.attrIdAndValueId.split(':')
          prev.push({ attrId, valueId })
        }
        return prev;
      }, [])
      console.log('12');
      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [saleAttrId, saleAttrValueId] = item.attrIdAndValueId.split(':');
          prev.push({ saleAttrId, saleAttrValueId })
        }
        return prev
      }, [])
      console.log('123');
      skuInfo.skuImageList = imageList.map(item => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id,
        }
      })
      // 发请求
      console.log('1234');

      let result = await this.$API.spu.reqAddSku(skuInfo);
      console.log(result);

      if (result.code === 200) {
        this.$message({ type: "success", message: "添加SKU成功" });
        this.$emit("changeScene", {
          scene: 0,
          flag: "",
        });
      }

    },
    cancel() {
      this.$emit("changeScene", {
        scene: 0,
        flag: "",
      });
      // 清理数据
      // Object.assign: es6中新增的方法，可以合并对象
      // 组件实例this._data, 可以操作data当中的响应式数据
      // this.$options可以获取配置对象，配置对象的data函数执行，返回的响应式数据为空
      Object.assign(this._data, this.$options.data());
    },
    async getData(category1Id, category2Id, spu) {
      // 收集父组件给予的数据
      this.skuInfo.category3Id = spu.category3Id;
      this.skuInfo.spuId = spu.id;
      this.skuInfo.tmId = spu.tmId;
      this.spu = spu;
      // 开始发请求
      let result = await this.$API.spu.reqSpuImageList(spu.id);
      if (result.code === 200) {
        let list = result.data;
        // console.log(result.data);
        list.forEach((item) => {
          item.isDefault = 0;
        });
        this.spuImageList = list;
      }
      let result1 = await this.$API.spu.reqSpuSaleAttrList(spu.id);
      if (result1.code === 200) {
        // console.log(result1.data);
        this.spuSaleAttrList = result1.data;
      }
      let result2 = await this.$API.spu.reqAttrInfoList(
        category1Id,
        category2Id,
        spu.category3Id
      );
      if (result2.code === 200) {
        // console.log(result2.data);
        this.attrInfoList = result2.data;
      }
    },
    //table表格复选框按钮事件
    handleSelectionChange(params) {
      //这个参数是element-ui，自定义事件提供的，为，所有图片的信息
      this.imageList = params
    },

    //唯一的默认
    changeDefault(row) {
      this.spuImageList.forEach(item => {
        item.isDefault = 0
      })
      row.isDefault = 1;
      this.skuInfo.skuDefaultImg = row.imgUrl
    }
  },

};
</script>

<style></style>